package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;


    @Autowired
    private JedisPool jedisPool;


    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file){

        try {
            //获取文件的原始名称
            String filename = file.getOriginalFilename();
            //获取它的扩展名
            filename = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
            //调用七牛云进行文件上传
            QiniuUtils.upload2Qiniu(file.getBytes(), filename);

            //将上传上来的图片名称记录到redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);

            //返回成功信息，并将文件名传回 到前端
            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        }catch(Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }

    }




    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){

        try {
            setmealService.add(setmeal, checkGroupIds);

            //添加成功之后，要将图片名称从redis中移除
            if(setmeal.getImg()!=null){
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal.getImg());
            }


            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


    @GetMapping("/findPage")
    public Result findPage( QueryPageBean queryPageBean){

        try {
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }




    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){

        try {
            Setmeal setmeal=setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }



    @GetMapping("/findCheckGroupIdsBySetmeal/{id}")
    public Result findCheckGroupIdsBySetmeal(@PathVariable Integer id){
        try {
            List<Integer> checkgroupIds= setmealService.findCheckGroupIdsBySetmeal(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    @PutMapping("/edit")
    public Result edit(Integer[] checkGroupIds,@RequestBody Setmeal setmeal){
        try {
            //获取更新之前的图片名称，记录到redis中
            Setmeal setmeal1 = setmealService.findById(setmeal.getId());
            if(setmeal1.getImg()!=null){
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal1.getImg());
            }

            setmealService.edit(setmeal, checkGroupIds);

            //将更新之后的图片名从redis中移除
            if(setmeal.getImg()!=null){
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal.getImg());
            }


            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }


    @DeleteMapping("/{setmeaId}")
    public Result delete(@PathVariable Integer setmeaId){
        try {
            //删除之前获取图片的名称，并添加到redis中
            Setmeal setmeal = setmealService.findById(setmeaId);
            if(setmeal.getImg()!=null){
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal.getImg());
            }


            setmealService.delete(setmeaId);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }


   @GetMapping("/generateStaticHtml")
    public Result generateStaticHtml(){
       try {
           setmealService.generateStaticHtml();
           return new Result(true,"静态页面生成成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"静态页面生成失败");
       }
   }

}
